<div class="easyui-layout" data-options="fit:true">

    <div data-options="region:'north',border:false" style="height:80px;width: 920px;padding:5px">
        <form id="accBookStock_query_form" style="height:80px;width: 920px">
            书名： <input class="easyui-textbox" id="accBookStock_query_form_name" name="name" />
            类型： <input id="accBookStock_query_form_type_id" name="type_id" />
            库存： <input class="easyui-numberbox" id="accBookStock_query_form_num_start" name="num_start" data-options="min:0" />
            至： <input class="easyui-numberbox" id="accBookStock_query_form_num_end" name="num_end" data-options="min:0" />
            <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-add'"
               style="width: 125px" onclick='accBookStockAdd();'>新增</a>
            <br>
            作者： <input id="accBookStock_query_form_author_id" name="author_id" />
            状态： <input id="accBookStock_query_form_status" name="status" />
            时间： <input class="easyui-datebox" data-options="editable:false,required:false,validType:'date'"
                      id="accBookStock_query_form_date_start" name="date_start" />
            至： <input class="easyui-datebox" data-options="editable:false,required:false,validType:'date'"
                     id="accBookStock_query_form_date_end" name="date_end" />
            <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-search'" style="width: 61px;height: 30px;"
               onclick='accBookStockQuery();'>查询</a>
            <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-clear'" style="width: 61px;height: 30px;"
               onclick='accBookStockQueryClear();'>重置</a>
        </form>
    </div>

    <!--        库存列表-->
    <div data-options="region:'center'" style="border:0;padding:5px;overflow:hidden;">
        <div style="width:920px;height:100%;overflow:auto;border:1px solid #ccc;float:left">
            <table id="accBookStock_bookStock_grid"></table>
        </div>
    </div>
</div>

<!--新增库存-->
<div id="accBookStock_add_dialog">
    <form id="accBookStock_add_form" method="post">
        <table style="margin-bottom:20px;width:100%">
            <tr>
                <td style="text-align: right;">书名:</td>
                <td><input id="accBookStock_add_form_book_id" name="book_id" required/></td>
            </tr>
            <tr>
                <td style="text-align: right;">库存:</td>
                <td><input id="accBookStock_add_form_num" name="num" class="easyui-numberbox" data-options="min:0" required/></td>
            </tr>
        </table>
    </form>
</div>

<!--加库存-->
<div id="accBookStock_edit_dialog">
    <form id="accBookStock_edit_form" method="post">
        <table style="margin-bottom:20px;width:100%">
            <tr>
                <td><input type="hidden" id="accBookStock_edit_form_id" name="id"/></td>
            </tr>
            <tr>
                <td style="text-align: right;">书名:</td>
                <td><input id="accBookStock_edit_form_book_name" name="book_name" class="easyui-textbox" required disabled/></td>
            </tr>
            <tr>
                <td style="text-align: right;">库存:</td>
                <td><input id="accBookStock_edit_form_num" name="num" class="easyui-numberbox" data-options="min:0" required/></td>
            </tr>
        </table>
    </form>
</div>

<script>
    $(function () {
        initBookStockComponents();
        initBookStockGrid();
    });

    function initBookStockComponents() {
        $("#accBookStock_query_form_type_id").combogrid({
            url: '/queryBookTypes',
            idField: 'id', textField: 'name',
            panelWidth: 160, panelHeight: 200,
            editable: true, mode: 'remote',
            columns: [
                [
                    {field: 'id', title: '编号', width: 50},
                    {field: 'name', title: '类型', width: 100}
                ]
            ],
            loadFilter: function (data) {
                if (data.data) return data.data;
                else return data;
            },
            onClickRow: function (value, row, index) {}
        });

        $("#accBookStock_query_form_author_id").combogrid({
            url: '/queryBookAuthors',
            idField: 'id', textField: 'name',
            panelWidth: 210, panelHeight: 200,
            editable: true, mode: 'remote',
            columns: [
                [
                    {field: 'id', title: '编号', width: 50},
                    {field: 'name', title: '姓名', width: 100},
                    {field: 'sex', title: '性别', width: 50,
                        formatter: function (value, row, index) {
                            if (value === 0) {
                                return '女';
                            } else if (value === 1) {
                                return '男';
                            } else {
                                return '';
                            }
                        }
                    }
                ]
            ],
            loadFilter: function (data) {
                if (data.data) return data.data;
                else return data;
            },
            onClickRow: function (value, row, index) {}
        });

        $("#accBookStock_query_form_status").combobox({
            valueField: 'id', textField: 'text',
            panelHeight: 99, editable: false,
            data: [
                {"id": 2, "text": "全部", selected: true},
                {"id": 1, "text": "可用"},
                {"id": 0, "text": "不可用"}
            ]
        });

        $("#accBookStock_add_form_book_id").combogrid({
            url: '/queryAllSysBooks',
            idField: 'id', textField: 'name',
            panelWidth: 400, panelHeight: 300,
            editable: true, mode: 'remote',
            pagination: true, pageSize: 200, pageList : [200],
            columns: [
                [
                    {field: 'id', title: '编号', width: 50},
                    {field: 'name', title: '书名', width: 150},
                    {field: 'type_name', title: '类型', width: 100},
                    {field: 'author_name', title: '作者', width: 80}
                ]
            ],
            invalidMessage: '库存已存在',
            validType: {
                // 请求服务器，校验书名是否可用
                remote:['/verifyBookStockId', 'bookName']
            },
            loadFilter: function (data) {
                if (data.data) return data.data;
                else return data;
            },
            onClickRow: function (value, row, index) {}
        });
    }

    function initBookStockGrid() {
        $('#accBookStock_bookStock_grid').datagrid({
            url: '/queryAllBookStocks',
            idField: 'id',
            // fitColumns: true,
            fit: true, scrollbarSize: 1, striped: true,
            checkOnSelect: false, selectOnCheck: false, singleSelect: true,
            pagination: true, pageSize: 15, pageList : [15, 30, 60 ],
            columns: [
                [
                    {field: 'id', title: '编码', width: 50, align: 'center', halign: 'center'},
                    {field: 'book_name', title: '书名', width: 150, align: 'center', halign: 'center'},
                    {field: 'book_type_name', title: '类型', width: 100, align: 'center', halign: 'center'},
                    {field: 'book_author_name', title: '作者', width: 100, align: 'center', halign: 'center'},
                    {field: 'num', title: '库存', width: 70, align: 'center', halign: 'center'},
                    {field: 'status', title: '状态', width: 70, align: 'center', halign: 'center',
                        formatter: function (value, row, index) {
                            if (value === 1) {
                                return '可用';
                            } else if (value === 0) {
                                return '不可用';
                            } else {
                                return '';
                            }
                        }
                    },
                    {field: 'ctime', title: '创建时间', width: 180, align: 'center', halign: 'center'},
                    {field: 'x', title: '操作', width: 150, align: 'center', halign: 'center',
                        formatter: function (value, row, index) {
                            return '<a href="javascript:void(0)" onclick="addBookStockButton(' + row.id + ')">加库存</a>' +
                                '<a href="javascript:void(0)" onclick="subBookStockButton(' + row.id + ')">|减库存</a>' +
                                '<a href="javascript:void(0)" onclick="deleteBookStockButton(' + row.id + ')">|删除</a>';
                        }
                    }
                ]
            ],
            loadFilter: function (data) {
                if (data.data) return data.data;
                else return data;
            },
            onLoadSuccess: function (node, data) {},
            onClickRow: function (index, row) {}
        });
    }

    // 新增库存
    function accBookStockAdd() {
        $('#accBookStock_add_dialog').dialog({
            title: '新增库存', iconCls: 'icon-add',
            width: 380, height: 350, modal: true, shadow: true,
            onClose: function () {
                $('#accBookStock_add_form').form('clear').form('reset');
            },
            buttons: [
                {
                    id: 'accBookStock_add_dialogsave',
                    text: '保存', iconCls: 'icon-save',
                    handler: addBookStock
                }, {
                    id: 'accBookStock_add_dialogclose',
                    text: '关闭', iconCls: 'icon-clear',
                    handler: function () {
                        $("#accBookStock_add_dialog").dialog("close");
                    }
                }
            ]
        });
    }

    function addBookStock() {
        if (!$('#accBookStock_add_form').form('validate')) {
            return;
        }

        $('#accBookStock_add_form').form('submit', {
            url: '/addBookStock',
            ajax: true,
            onSubmit: function () {},
            success: function (data) {
                data = JSON.parse(data);

                if (data.code !== 200) {
                    $.messager.alert("提示", "新增库存失败！", 'info');
                    return;
                }

                $.messager.show({title: '提示', msg: '新增库存成功！', timeout: 2000, showType: 'slide'});

                $("#accBookStock_add_dialog").dialog("close");
                $("#accBookStock_bookStock_grid").datagrid("reload");
            }
        });
    }

    function accBookStockQuery() {
        $("#accBookStock_bookStock_grid").datagrid('load', {
            name:$('#accBookStock_query_form_name').val(),
            type_id:$('#accBookStock_query_form_type_id').val(),
            num_start:$('#accBookStock_query_form_num_start').val(),
            num_end:$('#accBookStock_query_form_num_end').val(),
            author_id:$('#accBookStock_query_form_author_id').val(),
            status:$('#accBookStock_query_form_status').val(),
            date_start:$('#accBookStock_query_form_date_start').val(),
            date_end:$('#accBookStock_query_form_date_end').val()
        });
    }

    function accBookStockQueryClear() {
        $('#accBookStock_query_form_name').textbox('clear');
        $('#accBookStock_query_form_type_id').combogrid('clear');
        $('#accBookStock_query_form_num_start').numberbox('clear');
        $('#accBookStock_query_form_num_end').numberbox('clear');
        $('#accBookStock_query_form_author_id').combogrid('clear');
        $('#accBookStock_query_form_status').combobox('setValue', 2);
        $('#accBookStock_query_form_date_start').datebox('clear');
        $('#accBookStock_query_form_date_end').datebox('clear');
        $("#accBookStock_bookStock_grid").datagrid('load', {
            name: null,
            type_id: null,
            num_start: null,
            num_end: null,
            author_id: null,
            status: null,
            date_start: null,
            date_end: null
        });
    }

    // 加库存
    function addBookStockButton(stock_id) {
        opBookStockButton(stock_id, addBookStockNum, '加库存');
    }

    // 减库存
    function subBookStockButton(stock_id) {
        opBookStockButton(stock_id, subBookStockNum, '减库存');
    }

    // 操作库存
    function opBookStockButton(stock_id, opFun, title) {
        $('#accBookStock_bookStock_grid').datagrid("selectRecord", stock_id);
        var stock = $('#accBookStock_bookStock_grid').datagrid("getSelected");
        if (!stock) {
            $.messager.alert("提示", "请选择要编辑的库存！", 'info');
            return;
        }

        $("#accBookStock_edit_form").form('load', {id: stock.id, book_name: stock.book_name});

        $('#accBookStock_edit_dialog').dialog({
            title: title, iconCls: 'icon-edit',
            width: 380, height: 350, modal: true, shadow: true,
            onClose: function () {
                $('#accBookStock_edit_form').form('clear').form('reset');
            },
            buttons: [
                {
                    id: 'accBookStock_edit_dialogsave', text: title, iconCls: 'icon-save',
                    handler: opFun
                }, {
                    id: 'accBookStock_edit_dialogclose', text: '取消', iconCls: 'icon-clear',
                    handler: function () {
                        $("#accBookStock_edit_dialog").dialog("close");
                    }
                }
            ]
        });
    }

    // op 为 ADD/SUB
    function opBookStockNum(op) {
        if (!$('#accBookStock_edit_form').form('validate')) {
            return;
        }

        $('#accBookStock_edit_form').form('submit', {
            url: '/updateBookStockNum',
            queryParams: {op: op},
            ajax: true,
            onSubmit: function () {},
            success: function (data) {
                data = JSON.parse(data);

                if (data.code !== 200) {
                    $.messager.alert("提示", data.message, 'info');
                    return;
                }

                $.messager.show({title: '提示', msg: '操作库存成功！', timeout: 2000, showType: 'slide'});

                $("#accBookStock_edit_dialog").dialog("close");
                $("#accBookStock_bookStock_grid").datagrid("reload");
            }
        });
    }

    function addBookStockNum() {
        opBookStockNum('ADD')
    }

    function subBookStockNum() {
        opBookStockNum('SUB')
    }

    function deleteBookStockButton(stock_id) {
        $("#accBookStock_bookStock_grid").datagrid("selectRecord", stock_id);
        var row = $('#accBookStock_bookStock_grid').datagrid("getSelected");
        if (row.num !== 0) {
            $.messager.alert("提示", row.book_name + "：还有库存，不能删除", 'info');
            return;
        }

        $.messager.confirm('确认', '您确认想要删除该库存吗？', function (r) {
            if (!r) {return;}

            $.ajax({
                url: '/deleteBookStockById',
                type: "post",
                data: {id: stock_id},
                dataType: "json",
                async: false,
                success: function (data) {
                    if (data.code !== 200) {
                        $.messager.alert("提示", data.message, 'info');
                        return;
                    }

                    $.messager.show({title: '提示', msg: '删除库存成功！', timeout: 2000, showType: 'slide'});
                    $("#accBookStock_bookStock_grid").datagrid("reload");
                }
            });
        });
    }
</script>